package web.pio.test;

import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.record.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 读取文档如何使用HSSF侦听。
 */
public class EventExample implements HSSFListener
{
    private SSTRecord sstrec;

    public void processRecord(Record record)
    {
        // Record标识
        switch (record.getSid())
        {
            // the BOFRecord can represent either the beginning of a sheet or the workbook
            // BOFRecord 代表着excel的workbook或者sheet的开始。
            case BOFRecord.sid:
                BOFRecord bof = (BOFRecord) record;
                if (bof.getType() == bof.TYPE_WORKBOOK)
                {
                    System.out.println("######===>读取 [workbook]");
                    // assigned to the class level member
                } else if (bof.getType() == bof.TYPE_WORKSHEET)
                {
                    System.out.println("######===>读取 [sheet reference]");
                }
                break;
            case BoundSheetRecord.sid:  // 每一个Sheet
                BoundSheetRecord bsr = (BoundSheetRecord) record;
                System.out.println("######===>读取 [" + bsr.getSheetname() + "]  ==  行数[" + bsr.getRecordSize() + "]");
                break;
            case RowRecord.sid: // 每一行
                RowRecord rowrec = (RowRecord) record;
                System.out.println("######===>读取 [row]" + rowrec.getFirstCol() + " ## " + rowrec.getLastCol());
                break;
            case NumberRecord.sid:  // 数字单元格
                NumberRecord numrec = (NumberRecord) record;
                System.out.println("数字单元格====>" + numrec.getValue()
                        + " at row " + numrec.getRow() + " and column " + numrec.getColumn());
                break;
                // SSTRecords store a array of unique strings used in Excel.
                // SSTRecords 里面存储了Excel中唯一字符串的信息
            case SSTRecord.sid:
                sstrec = (SSTRecord) record;
                for (int k = 0; k < sstrec.getNumUniqueStrings(); k++)
                {
                    System.out.println("######===>表头信息[" + k + " = " + sstrec.getString(k) + "]");
                }
                break;
            case LabelSSTRecord.sid:// 字符串单元格
                LabelSSTRecord lrec = (LabelSSTRecord) record;
                System.out.println("字符串单元格===>"
                        + sstrec.getString(lrec.getSSTIndex()));
                break;
        }
    }

    /**
     * Read an excel file and spit out what we find.
     *
     * @param args      Expect one argument that is the file to read.
     * @throws java.io.IOException  When there is an error processing the file.
     */
    public static void main(String[] args) throws IOException
    {
        System.out.println("##############===>");
        // 读取文件流
        FileInputStream fin = new FileInputStream("D:\\[工程临时]\\[中学选课与学分鉴定]\\价格表整理11.xls");
        // 创建POIFSFileSystem org.apache.poi.poifs.filesystem.Filesystem
        POIFSFileSystem poifs = new POIFSFileSystem(fin);
        // 创建Excel（03excel）的输出流
        InputStream din = poifs.createDocumentInputStream("Workbook");
        // 创建HSSFRequest对象
        HSSFRequest req = new HSSFRequest();
        // lazy listen for ALL records with the listener shown above
        req.addListenerForAllRecords(new EventExample());
        // 创建HSSF事件工厂
        HSSFEventFactory factory = new HSSFEventFactory();
        // 基于文档流，处理我们的时间
        factory.processEvents(req, din);
        // 关闭文件流
        fin.close();
        // 关闭文档流
        din.close();
        System.out.println("##############===>");
    }
}